package com.samshow.common;

import com.samshow.baseException.BaseException;
import com.samshow.baseVo.ErrorInfo;
import com.samshow.baseVo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by niwb on 2017/3/6.
 */
class GlobalExceptionHandler {
    @Autowired
    private Environment environment;
    public static final String DEFAULT_ERROR_CODE = "1000";

    @ExceptionHandler(value = RuntimeException.class)
    @ResponseBody
    public Result defaultErrorHandler(HttpServletRequest req, Exception e) throws Exception {
        String message = environment.getProperty(DEFAULT_ERROR_CODE);
        Result errorInfo = new ErrorInfo(DEFAULT_ERROR_CODE, message);
        return errorInfo;
    }


    @ExceptionHandler(value = BaseException.class)
    @ResponseBody
    public Result customerErrorHandler(HttpServletRequest req, BaseException e) throws Exception {
        String message = environment.getProperty(e.getCode());
        Result errorInfo = new ErrorInfo(e.getCode(), message);
        return errorInfo;
    }
}